<?php


namespace app\api\controller;

use app\BaseController;
use app\Request;
use think\facade\Db;
use think\exception\ValidateException;

class Log extends BaseController
{
    public function addRequestErrorLog() 
    {
        $postField = 'device_id,content,sign';
		$postData = $this->request->only(explode(',',$postField),'post',null);
        if (empty($postData['content'])) {
            throw new ValidateException ('内容不能为空');
        }
        if (empty($postData['device_id'])) {
            throw new ValidateException ('设备不能为空');
        }
        if (empty($postData['sign'])) {
            throw new ValidateException ('签名不能为空');
        }
        $device = Db::name('device')->where('assoc_device_id', $postData['device_id'])->find();
        if (empty($device)) {
            throw new ValidateException ('无效的设备');
        }
        //验证签名
        $sign = $postData['sign'];
        unset($postData['sign']);
        $makeSign = MakeSign($postData, config('my.public_api_key'));
        if ($sign != $makeSign) {
            throw new ValidateException ('签名错误');
        }
        $insert = [
            'device_id' => $postData['device_id'],
            'content' => is_array($postData['content'])?json_encode($postData['content'], JSON_UNESCAPED_UNICODE):$postData['content'],
            'create_time' => time()
        ];
        Db::name('request_error_log')->insert($insert);
        $data['status'] = 200;
		$data['data'] = [];
		return json($data);
    }
}